#include "movieDownloadProgressMessage.h"
#include <rapidjson/document.h>
#include "jsonUtils.h"

MovieDownloadProgressMessage::MovieDownloadProgressMessage(MtpProtocol* packet): MtpMessage(packet),movieId(0),status(0),percent(0)
{

}

MovieDownloadProgressMessage::MovieDownloadProgressMessage(const int64_t& movieId, const int& status, const int& percent, const String& message)
	: MtpMessage(new MtpProtocol(L"movieDownloadProgress")),movieId(movieId),status(status),percent(percent),message(message)
{
}

MovieDownloadProgressMessage::MovieDownloadProgressMessage(const int64_t& movieId, const int& status, const int& percent)
	: MtpMessage(new MtpProtocol(L"movieDownloadProgress")), movieId(movieId), status(status), percent(percent)
{
}

void MovieDownloadProgressMessage::decodeBody()
{
	MtpMessage::decodeBody();
	if (!jsonContent.empty()) {
		JsonDoc doc(rapidjson::kObjectType);
		doc.Parse(jsonContent.c_str());
		JsonUtils::loadLong(doc, L"movieId", movieId);
		JsonUtils::loadInt(doc, L"status", status);
		JsonUtils::loadInt(doc, L"percent", percent);
		JsonUtils::loadStr(doc, L"message", message);
		JsonUtils::loadInt(doc, L"totalCnt", totalCnt);
		JsonUtils::loadInt(doc, L"current", current);
	}
}

void MovieDownloadProgressMessage::encodeBody()
{
	JsonDoc doc(rapidjson::kObjectType);
	doc.AddMember(L"movieId", movieId, doc.GetAllocator());
	doc.AddMember(L"status", status, doc.GetAllocator());
	doc.AddMember(L"percent", percent, doc.GetAllocator());
	JsonUtils::addString(doc, L"message", message, doc.GetAllocator());
	doc.AddMember(L"totalCnt", totalCnt, doc.GetAllocator());
	doc.AddMember(L"current", current, doc.GetAllocator());
	jsonContent = JsonUtils::jsonStr(doc);
	MtpMessage::encodeBody();
}

void MovieDownloadProgressMessage::setTotal(const int& total)
{
	this->totalCnt = total;
}

void MovieDownloadProgressMessage::setCurrent(const int& current)
{
	this->current = current;
}
